package com.example.springboot_jpa.respository;

import com.example.springboot_jpa.jopo.Comment;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment,Integer> {
    //@query表示增,删，改，查操作的注解,nativeQuery参数表示开启原生sql语句语法规则
    //Pageable表示一个分页的对象，指定起始的数据和最终数据
    @Query(value = "select * from t_comment where a_id=?1",nativeQuery = true)
    List<Comment> getCommentPage(Integer a_id, Pageable pageable);//第一页的数据，每页数据显示3条
    @Transactional
    @Modifying
    @Query("update t_comment c set c.author=?1 where c.id=?2")
    int updateComment(String author,Integer id);
    @Transactional//开启事务
    @Modifying //删除和修改操作必须添加此注解  idea兼容性差
    @Query("delete t_comment c where c.id=?1")
    int deleteComment(Integer id);

    @Transactional//开启事务
    @Modifying //删除和修改操作必须添加此注解  idea兼容性差
    @Query(value = "insert into t_comment(content,author,a_id)  value (?1,?2,?3)",nativeQuery = true)
    int insertComment(String content,String author,Integer articleId);

}
